package com.itboye.ihomebank.activity.finance;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.ZhiMaBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.net.URLEncoder;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class ZhiMaActivity extends BaseOtherActivity implements Observer {
    TextView tv, ss;

    UserPresenter userPresenter;

    String url,biz_no;
    String uid;

    @Override
    protected int layout_id() {
        return R.layout.activity_zhi_ma;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        finish();
        userPresenter = new UserPresenter(this);
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv:
                userPresenter.zhiMa(uid);
                break;
            case R.id.ss:
                doVerify(url);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 101) {
//            userPresenter.zhiMaChaXun(uid,biz_no);
//        }
    }

    /**
     * 启动支付宝进行认证
     *
     * @param url 开放平台返回的URL
     */
    private void doVerify(String url) {
        if (hasApplication()) {
            Intent action = new Intent(Intent.ACTION_VIEW);
            StringBuilder builder = new StringBuilder();
            // 这里使用固定appid 20000067
            builder.append("alipays://platformapi/startapp?appId=20000067&url=");
            builder.append(URLEncoder.encode(url));
            action.setData(Uri.parse(builder.toString()));
            startActivityForResult(action, 101);
        } else {
            // 处理没有安装支付宝的情况
            new AlertDialog.Builder(this)
                    .setMessage("是否下载并安装支付宝完成认证?")
                    .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent action = new Intent(Intent.ACTION_VIEW);
                            action.setData(Uri.parse("https://m.alipay.com"));
                            startActivity(action);
                        }
                    }).setNegativeButton("算了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
    }

    /**
     * 判断是否安装了支付宝
     *
     * @return true 为已经安装
     */
    private boolean hasApplication() {
        PackageManager manager = getPackageManager();
        Intent action = new Intent(Intent.ACTION_VIEW);
        action.setData(Uri.parse("alipays://"));
        List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
        return list != null && list.size() > 0;
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.zhima_success) {
                ZhiMaBean bean = (ZhiMaBean) entity.getData();
                url = bean.getUrl();
                biz_no = bean.getBiz_no();
                tv.setText(url);
                //  ByAlert.alert(bean.getUrl());
            } else if (entity.getEventType() == UserPresenter.zhima_fail) {
                ByAlert.alert(entity.getData());
            }
            else if (entity.getEventType() == UserPresenter.zhima_save_success) {
                ByAlert.alert(entity.getData());
            }
            else if (entity.getEventType() == UserPresenter.zhima_save_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
